package com.atguigu.gmall.pms.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;

/**
 * @author fan
 * @since 2021-09-10
 */
@Slf4j
@Configuration
public class RabbitConfig {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void init(){
        // 确认消息是否到达交换机
        this.rabbitTemplate.setConfirmCallback(( correlationData,  ack,  cause)->{
            if(!ack){
                //消息唯一标识、确认结果、失败原因
                log.error("消息没有到达交换机。原因{}",cause);
            }
        });
        this.rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey)->{
            log.error("消息没有到达队列。交换机：{}，路由键：{}， 消息内容：{}。错误文本：{}", exchange, routingKey, new String(message.getBody()), replyText);
        });

    }
}
